<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<%
	List<String> names = new ArrayList<String>();
	Cookie[] arr = request.getCookies();
	if (arr != null) {
		for (Cookie c : arr) {
			if (("cookid" + c.getValue()).equals(c.getName())) {
				names.add(c.getValue());
			}
		}
	}
	pageContext.setAttribute("goodnames", names);
%>
<link
	href="${pageContext.request.contextPath}/javascript/bootstrap/css/bootstrap.min.css"
	rel="stylesheet" />
<h3>结帐页面</h3>

<body>
	<div class="table-responsive m-t">
		<h2 style="text-align: center">购物车</h2>
		<br>
		<table id="mytable" class="table table-condensed">
			<tr class="active">
				<td>商品编号</td>
				<td>名称</td>
				<td>价格</td>
				<td>操作</td>
			</tr>
			<c:forEach items="${goodnames}" var="g">
				<tr class="active">
					<td class="active">${g}</td>
					<td class="active"></td>
					<td class="active">&yen;</td>
					<td class="active"><a id=${g } href="#"
						onclick='getData(${g})'></a> &nbsp;&nbsp;&nbsp;&nbsp; <a
						class="btn btn-default" href="#" onclick='remove(${g})'> 移除</a></td>
				</tr>

			</c:forEach>

		</table>
	</div>

	<table class="table invoice-total">
		<tbody>
			<tr>
				<td><strong>总价&yen;：</strong></td>
				<td id="total">0</td>
		</tbody>
	</table>
	<div class="text-right">
		<button class="btn btn-primary">
			<i class="fa fa-dollar" onclick="buy()">付款</i>
		</button>
	</div>

	<div class="well m-t">
		<strong>注意：</strong> 请在7日内完成付款，否则订单会自动取消。
	</div>
</body>
<script
	src="${pageContext.request.contextPath}/javascript/myjavascript/jQuery-1.12.4.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script
	src="${pageContext.request.contextPath}/javascript/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
   
   <c:forEach items="${goodnames}" var="g">
   	if(${g}!=null){
   	   document.getElementById(${g}).click();
   	}
    </c:forEach>
   
	function remove(id) {
		$.ajax({
			type : "get",
			url : "remove.jsp",
			data : {
				"id" : id
			},
			success : function(msg) {
				location.reload(true);//刷新该页面
				alert("删除成功");
				//$('#mytable').bootstrapTable('refresh');
			}
		})
	}	
	function getData(id) {
		 var tab = document.getElementById("mytable"); //通过ID选择表格
	       
	       //alert(rows);
	       //var t =$("#mytable").find("tr:eq("+ n +") td:eq(0)").html();
	    	   $.ajax({
	   			type : "get",
	   			url : "../ProductServlet",
	   			data : {
	   				"op" : "doSearchByCookid",
	   				"id" : id
	   			},
	   			dataType : "json",
	   			success : function(jsonArr) {
	   				//alert(jsonArr[0]);
	   				var rows = tab.rows.length; //得到表格行数
	   				for(var n = 1;n < rows; n++){
	   					var s = $("#mytable").find("tr:eq("+ n +") td:eq(0)").html();
	   					if(jsonArr[0].productID == s ){
	   						$("#mytable").find("tr:eq("+ n +") td:eq(1)").html(jsonArr[0].product_name);
	   						$("#mytable").find("tr:eq("+ n +") td:eq(2)").html("&yen;"+jsonArr[0].sales_price);
	   						var t = $("#total").text();
	   						t=parseFloat(t);
	   						t+=jsonArr[0].sales_price;
	   						console.log(t)
	   						$("#total").text(t.toFixed(2));
	   					}
	   							
	   				}
	   			}
	   		})
  	    
	    	    
	}
	
      function buy() {
		alert("购买成功");
	}
        

    
</script>

</html>